const Task = function(properties, action, callback) {  
    Object.assign(this, properties || {}, {
        name: undefined,
        args: undefined,
        counter: 0,
        type: undefined,
        depend: undefined,
        interval: undefined,
        retry: undefined,
        next: Date.now(),
        action: action || (() => console.log("执行任务默认处理")),
        callback: callback || (() => true)
    });
    this.run = function(context) {
        let current = Date.now()
        //先置为一分钟后,卡住这个任务，防止处理中会再调度到这个任务
        this.next = current + 60 * 1000
        console.log("任务【" + this.name + "】开始执行")
        let executeResult = this.action(this.args, context)
        console.log("任务【" + this.name + "】执行结果：" + JSON.stringify(executeResult))
        let processResult = this.callback(this.type, executeResult)
        console.log("任务【" + this.name + "】处理结果：" + JSON.stringify(processResult) + "," + (current + this.interval))
        if (processResult || (this.retry && this.counter >= this.retry)) {
            this.next = -1
        } else {
            this.next = current + this.interval
        }
        this.counter = this.counter + 1
    },
    this.ready = function() {
        return this.next <= Date.now()
    },
    this.completed = function() {
        if (this.next == -1) {
            return true
        }
        return false
    }
}
exports.createTask = (properties, action, callback) => new Task(properties, action, callback)
